package com.ukao.steward.ui.valetRunners.bingCard;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baoyz.actionsheet.ActionSheet;
import com.bigkoo.pickerview.TimePickerView;
import com.ukao.steward.R;
import com.ukao.steward.base.MvpFragment;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.dialog.PickerViewDialog;
import com.ukao.steward.pesenter.valetRunners.BuildUserPesenter;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.CountDownUtils;
import com.ukao.steward.util.RegularUtils;
import com.ukao.steward.util.T;
import com.ukao.steward.view.valetRunners.BuildUserView;
import com.ukao.steward.widget.StateButton;

import java.util.Date;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.ukao.steward.util.MyDateUtils.getTime;

/**
 * 创建用户
 */
public class BuildUserFragment extends MvpFragment<BuildUserPesenter> implements BuildUserView {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    Unbinder unbinder;
    @BindView(R.id.btn_verify)
    TextView btnVerify;
    @BindView(R.id.commit)
    StateButton commit;
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.code)
    EditText code;
    @BindView(R.id.product_name)
    EditText name;
    @BindView(R.id.sex)
    TextView sex;
    @BindView(R.id.birthday)
    TextView birthday;
    @BindView(R.id.hite)
    TextView hite;

    private String mParam1;
    private boolean mParam2;
    private CountDownUtils mCountDownUtils;
    private String timeType = "yyyy-MM-dd";
    private String[] sexArray = new String[]{"男", "女"};
    private String mPhoneParam;
    private   String  sexId;/*上传服务器性别id*/
    private boolean isStoreCreatOrderEnter;

    public static BuildUserFragment newInstance(String phone, boolean  isStoreCreatOrderEnter) {
        BuildUserFragment fragment = new BuildUserFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, phone);
        args.putBoolean(ARG_PARAM2, isStoreCreatOrderEnter);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mPhoneParam = getArguments().getString(ARG_PARAM1);
            isStoreCreatOrderEnter = getArguments().getBoolean(ARG_PARAM2);
        }
    }

    @Override
    protected BuildUserPesenter createPresenter() {
        return new BuildUserPesenter(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_build_user, container, false);
        unbinder = ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    protected void initView() {
        phone.setText(mPhoneParam);
        hite.setText("手机号 "+mPhoneParam+" 是新客户，请先创建客户再绑卡");
        mCountDownUtils = new CountDownUtils(btnVerify, "获取验证码", 60, 1);
        setStatusBarTintColor(R.color.normal_blue);
        titleLayout.setBackgroundColor(getColors(R.color.colorPrimary));
        title.setText("创建客户");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.back_btn, R.id.title, R.id.btn_verify, R.id.commit, R.id.birthday, R.id.sex})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sex:
                PickerViewDialog.getInstance().showActionSheetDialog(getChildFragmentManager(), sexArray, mActionSheetListener);
                break;
            case R.id.back_btn:
                finish();
                break;
            case R.id.title:
                break;
            case R.id.btn_verify:

                String strphonecode = getText(phone);
                if (!RegularUtils.isMobileSimple(strphonecode)) {
                    T.show("请输入正确手机号");
                    return;
                }
                mvpPresenter.testGetCode(strphonecode);

                break;
            case R.id.birthday:
                String strsbirthday = getText(birthday);
                PickerViewDialog.getInstance().showDatePicker(strsbirthday, timeType, _mActivity, mTimePickerView);
                break;
            case R.id.commit:
                String strphone = getText(phone);
               // String strcode = getText(code);
                String strname = getText(name);
                String strsex = getText(sex);
                String strbirthday = getText(birthday);
                if (!RegularUtils.isMobileSimple(strphone)) {
                    T.show("请输入正确手机号");
                    return;
                }  else if (CheckUtils.isEmpty(strname)) {
                    T.show("请输入姓名");
                    return;
                } else if (CheckUtils.isEmpty(strsex)) {
                    T.show("请选择性别");
                    return;
                }

                HashMap mHashMap = new HashMap<String, String>();
                mHashMap.put("name",strname);
                mHashMap.put("phone",strphone);
                mHashMap.put("sex",sexId);
                mHashMap.put("birthday",strbirthday);
              //  mHashMap.put("smsCode",strcode);
                mHashMap.put("accessToken", SaveParams.getToken());
                mvpPresenter.userInfoAdd(mHashMap);
                break;


        }
    }

    TimePickerView.OnTimeSelectListener mTimePickerView = new TimePickerView.OnTimeSelectListener() {
        @Override
        public void onTimeSelect(Date date, View v) {
            birthday.setText(getTime(date, timeType));
        }
    };

    ActionSheet.ActionSheetListener mActionSheetListener = new ActionSheet.ActionSheetListener() {
        @Override
        public void onDismiss(ActionSheet actionSheet, boolean isCancel) {

        }

        @Override
        public void onOtherButtonClick(ActionSheet actionSheet, int index) {
            sex.setText(sexArray[index]);
            sexId = index == 0 ? "1" : "2";/*男为1*/

        }
    };

    @Override
    public void loadVerifyCode() {
        mCountDownUtils.start();
    }

    /**
     * 添加客户
     */
    @Override
    public void userInfoAddSucceed(String  msg) {
        T.show(msg);
        finish();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mCountDownUtils.cancels();
    }
}
